package com.chenyun.boot.web.application;

import cn.hutool.core.thread.ThreadUtil;
import com.chenyun.common.service.IStartUpService;
import com.chenyun.common.utils.YidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * 程序启动完成执行
 *
 * @author: Lonni
 * @date: 2022/9/30 0030 16:16
 */
@Component
@Slf4j
public class ApplicationStartEvent implements ApplicationRunner {

    /**
     * 拿到服务类
     */
    @Autowired(required = false)
    private List<IStartUpService> startUpServices;


    @Override
    public void run(ApplicationArguments args) throws Exception {

        //开始线程去执行,不要影响程序启动
        log.info("程序启动完成后,异步执行其他预处理任务");
        //初始化唯一id
        YidUtil.me.init();
        ThreadUtil.execAsync(()->{
            for (IStartUpService startUpService : startUpServices) {
                try {
                    startUpService.execute();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });



    }


}
